//用于显示测试计划的测试步骤的grid
var testDesignStepGrid=Ext.create('Ext.grid.Panel', {
    store: testDesignStepStore,
    dockedItems: [{
		xtype: 'toolbar',
		items: [{
			text: '添加',
            tooltip: '添加测试步骤',
            itemId:'testDesignAddStepButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: {
                click: function () {
                	var oldRecord=testDesignStepStore.getData().items;
                	var frontNode,backNode;
                	var id=Ext.create("MyApp.data.MyModelX").data.id.replace(/-/g, "");
                	if(oldRecord.length==0){//如果本来就没有的话就直接添加首尾标识符就好了
                		frontNode='begin';
                		backNode='end';
                	}else{//不然就跟在已有列表的最后一个
                		backNode=oldRecord[oldRecord.length-1].data.backNode;
                		oldRecord[oldRecord.length-1].data.backNode=id;
                		frontNode=oldRecord[oldRecord.length-1].data.id;
                	}
                	var newRecord = {
							"id":id,
							"name": "",
							"description": "",
							"expectedResult":"",
							"frontNode":frontNode,
							"backNode":backNode
					};
                	testDesignStepStore.add(newRecord);
                }
            }
		}, '-', {
			text: '删除',
            tooltip: '删除测试步骤',
            itemId:'testDesignDeleteStepButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: {
                click: function () {
                	var item = testDesignStepGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
                	if(item.length==0)
                		Ext.Msg.alert('错误', '请选择要删除的测试步骤！');
                	else{
                		Ext.MessageBox.confirm('删除', '你确定要将该测试步骤删除吗', function(btn){
                	        if (btn === 'yes') {
                            	var record=testDesignStepStore.getById(item.items[0].data.id);
                            	testDesignStepStore.remove(record);
                	        }
                	    });
                	}
                }
            }
		}, '-',{
			text: '上移',
            tooltip: '上移测试步骤',
            icon: 'static/images/icons/fam/arrow_up.png',
            itemId:'testDesignUpStepButton',
            listeners: {
                click: function () {
                	testDesignStepUpOrDown(true);
                }
            }
		}, '-', {
			text: '下移',
            tooltip: '下移测试步骤',
            itemId:'testDesignDownStepButton',
            icon: 'static/images/icons/fam/arrow_down.png',
            listeners: {
                click: function () {
                	testDesignStepUpOrDown(false);
                }
            }
		}]
    }],
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '名称',
            width: 100,
            dataIndex:'name',
            sortable:false,
            editor: {
                completeOnEnter: true,//是否允许用回车结束输入
                field: {
                    xtype: 'textfield',
                    allowBlank: false//是否允许为空
                }
            }
        }, {
            text: '内容',
            dataIndex: 'description',
            flex: 1,
            sortable:false,
            renderer: function (v, m, r) {
            	if(v.length<15)
            		return v;
            	else
            		return '内容过长,请展开查看';
            },
            listeners:[{
				click:function(a,b,c,d,e,f){
					if(testDesignCreateOrUpdate==0||testDesignTabPanel.canUpdate){//如果允许编辑的话
						testDesignStepEditorWin.show();
						window.addEventListener('message', testDesignPutEditDataIntoGrid, false);//添加监听
						testDesignListenFlag=true;
						testDesignListenRowData=f;
						testDesignStepEditorIFrame.getFrame().contentWindow.postMessage({
					        status: 5,
					        text:f.data.description
					    }, zuulUrl+'/'+webServiceName+'/editor');//将点击的测试步骤的值显示到编辑器上
					}
				}
			}]
        }, {
            text: '期望结果',
            dataIndex: 'expectedResult',
            flex: 0.5,
            sortable:false,
            editor: {
                completeOnEnter: true,//是否允许用回车结束输入
                field: {
                    xtype: 'textfield',
                    allowBlank: false//是否允许为空
                }
            }
        }],
        plugins: {
			cellediting:{
				clicksToEdit: 1,
				listeners:[{
					beforeedit :function(){
						return testDesignCreateOrUpdate==0||testDesignTabPanel.canUpdate;//如果是新建操作或者允许编辑的话
					}
				}]
			},
			rowexpander: {
				rowBodyTpl: new Ext.XTemplate(
					'{description}'
				)
			}
		}
});

//用于显示测试计划的关联测试需求的grid
var testDesignTestRequirementGrid=Ext.create('Ext.grid.Panel', {
    store: testDesignTestRequirmentStore,
    dockedItems: [{
		xtype: 'toolbar',
		items: [{
			text: '添加',
            tooltip: '添加关联测试需求',
            itemId:'testDesignReqGridAddButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: {
                click: function () {
                	testDesignReqStore.proxy.url=zuulUrl+'/'+testServiceName+'/common/testRequirement/project/'+testDesignProject
                	testDesignReqStore.reload();
                	testDesignReqTreeGridWin.show();
                }
            }
		}, '-', {
			text: '删除',
            tooltip: '删除关联测试需求',
            itemId:'testDesignReqGridDeleteButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: {
                click: function () {
                	Ext.MessageBox.confirm('删除', '你确定要将该关联测试需求删除吗', function(btn){
            	        if (btn === 'yes') {
            	        	var item = testDesignTestRequirementGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
                        	var record=testDesignTestRequirmentStore.getById(item.items[0].data.id);
                        	testDesignTestRequirmentStore.remove(record);
            	        }
            	    });
                }
            }
		}]
    }],
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '名称',
            width: 100,
            dataIndex:'title'
        }, {
            text: '编写者',
            dataIndex: 'author',
            flex: 0.5
        }, {
            text: '内容',
            dataIndex: 'description',
            flex: 1,
            renderer: function (v, m, r) {
            	if(v.length<15)
            		return v;
            	else
            		return '内容过长,请展开查看';
            }
        }],
        plugins: {
			rowexpander: {
				rowBodyTpl: new Ext.XTemplate(
					'{description}'
				)
			}
		}
});

//用于显示测试计划的审核记录
var testDesignCheckLogGrid=Ext.create('Ext.grid.Panel', {
    store: testDesignCheckLogStore,
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '状态',
            width: 100,
            renderer: function (v, record) {
                if (record.record.data.isFinish == 1) {
                    if (record.record.data.isPass == 1)
                        return '审核通过';
                    else
                        return '审核不通过';
                } else
                    return '审核中';
            },
        }, {
            text: '审核人',
            dataIndex: 'checkMan',
            flex: 0.5
        }, {
            text: '审核日期',
            dataIndex: 'checkDate',
            flex: 0.5,
            renderer: function (value) {
                return value.replace('T', '  ');
            }
        }, {
            text: '查看批注',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 100,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看批注',
                handler: function (grid, rowIndex, colIndex) {
                    var id = testDesignCheckLogStore.getAt(rowIndex).data.id;
                    Ext.Ajax.request({//然后去后台请求该审核记录的批注内容
                        url: zuulUrl + '/' + testServiceName + '/testDesignCheck/common/annotate/' + id,
                        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                        success: function (response, opts) {
                            var data = response.responseText;
                            testDesignCheckLogAnnotatoWin.show();
                            testDesignCheckLogAnnotatoWin.down('#testDesignCheckLogAnnotatoPanel').setHtml('<p><b>' + data + '</b></p>');
                        },
                        failure: function (response, opts) {
                            Ext.Msg.alert('失败', '该操作暂时不可用！');
                        }
                    });
                }
            }]
        }]
});